#!/bin/bash
# create, edit and view month log
# usage: monthlog OPTIONS
# example: monthlog
#          monthlog "last month" 
#          monthlog 2008-08

if [ ! -f ~/.calendar ]; then
  echo -ne "calendar_path="
  read calendar_path
  echo -ne "version_control=(none, svn, git, cvs)"
  read version_control
  echo "calendar_path=$calendar_path" >> ~/.calendar
  echo "version_control=$version_control" >> ~/.calendar
else
  while read line; do
    case $line in
      "calendar_path="*) calendar_path=${line##calendar_path=};;
      "version_control="*) version_control=${line##version_control=};;
    esac
  done < ~/.calendar
fi

cd $calendar_path
if [ "$version_control" = "svn" ]; then
  svn up
fi

if [ $# -eq 0 ]; then
  logfilename=`date +%Y`-`date +%m`
else
  if [ "$1" = "update" ]; then
    if [ "$version_control" = "svn" ]; then
      svn update
    fi
    exit
  else 
    if [ "$1" = "up" ]; then
      if [ "$version_control" = "svn" ]; then
        svn up
        exit
      fi
    else
      logfilename=`date --date="$1" +%Y`-`date --date="$1" +%m`
    fi
  fi
fi

if [ -f $logfilename ]; then
  vim $logfilename
  if [ "$version_control" = "svn" ]; then
    svn commit -m "update $logfilename"
  fi
else
  echo $logfilename >> $logfilename
  vim $logfilename
  if [ "$version_control" = "svn" ]; then
    svn add $logfilename
    svn commit -m "add $logfilename"
  fi
fi

